package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.NewsDetail;
import com.qf.mapper.NewsDetailMapper;
import com.qf.service.NewsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lenovo
 * @version V1.0
 * @Date 2021/10/6 0:13
 */
@Service
public class NewsDetailServiceImpl implements NewsDetailService {

    @Autowired
    private NewsDetailMapper newsDetailMapper;

    @Override
    public List<NewsDetail> findAllByTitle(String title) {

        return newsDetailMapper.findAllByTitle(title);
    }

    @Override
    public PageInfo<NewsDetail> findAllByTitleAndPage(Integer num, Integer size, String title) {

        PageHelper.startPage(num,size);

        List<NewsDetail> detailList = newsDetailMapper.findAllByTitle(title);

        PageInfo<NewsDetail> pageInfo = new PageInfo<>(detailList);

        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void delete(Integer id) {
        int i = newsDetailMapper.deleteByPrimaryKey(id);
        if (i != 1){
            System.out.println("删除失败");
            throw new RuntimeException("删除失败");
        }
    }
}
